package com.shop.manager.controller;

//import com.shop.manager.entity.Specification;
//import com.shop.manager.http.HttpResult;
//import com.shop.manager.http.PageResult;
import com.base.common.entity.Specification;
import com.base.common.http.HttpResult;
import com.base.common.http.PageResult;
import com.shop.manager.service.SpecificationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * @Description 规格增删改查
 */
@RestController
@RequestMapping("/specification")
public class SpecificationController {

    /**
     * @Description 注入service对象
     */
    @Autowired
    private SpecificationService specificationService;

    /**
     * @Description 添加规格
     * @param specification 规格对象
     * @return
     */
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public HttpResult add(@RequestBody(required = false) Specification specification){
        try {
            specificationService.addSpecification(specification);
            return HttpResult.ok("添加规格成功!");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return HttpResult.error("添加规格失败!");
    }

    /**
     * @Description 批量删除规格
     * @param ids 规格id的数组
     * @return
     */
    @RequestMapping("/delete")
    public HttpResult delete(@RequestBody(required = false) Long[] ids){
        try {
            specificationService.deleteSpecification(Arrays.asList(ids));
            //删除成功
            return HttpResult.ok();
        } catch (Exception e) {
            e.printStackTrace();
            //删除异常
            return HttpResult.error();
        }
    }

    /**
     * @Description 修改规格
     * @param specification 规格对象
     * @return
     */
    @RequestMapping("/update")
    public HttpResult update(@RequestBody(required = false) Specification specification){
        try {
            specificationService.updateSpecification(specification);
            return HttpResult.ok("修改规格成功!");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return HttpResult.error("修改规格失败!");
    }

    /**
     * @Description 根据ID查询规格
     * @param id 规格id
     * @return
     */
    @RequestMapping("/getById")
    public HttpResult getById(@RequestParam("id") Long id){
        return HttpResult.ok(specificationService.getBySpecificationId(id));
    }

    /**
     * @Description 条件分页查询
     * @param pageNum 当前页码
     * @param size 当前页条数
     * @param specification 查询条件对象
     * @return
     */
    @RequestMapping("/getAll")
    public HttpResult getAll(@RequestParam("pageNum") Integer pageNum, @RequestParam("size") Integer size, @RequestBody(required = false) Specification specification){
        try {
            //分页查询
            PageResult result = specificationService.getAllSpecification(pageNum, size, specification);
            //查询成功
            return HttpResult.ok(result);
        } catch (Exception e) {
            e.printStackTrace();
            //查询异常
            return HttpResult.error("查询规格异常");
        }
    }

    /**
     * @Description 模糊查询规格
     * @param specification 规格对象
     * @return
     */
    @RequestMapping("/findByWhere")
    public HttpResult findByWhere(@RequestBody(required = false) Specification specification){
        return HttpResult.ok(specificationService.findByWhere(specification));
    }

    /**
     * @Description 查询全部规格
     * @return
     */
    @RequestMapping("/specificationList")
    public HttpResult list(){
        List<Map<String, Object>> list = specificationService.specificationList();
        return HttpResult.ok(list);
    }


}
